package com.pet.backend.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.pet.backend.pojo.Goods;
import com.pet.backend.pojo.Vo;
import com.pet.backend.service.impl.GoodsServiceImpl;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * <p>
 * 商品管理
 * </p>
 *
 * @author pet
 * @since 2024-07-13
 */
@CrossOrigin
@RestController
@RequestMapping("/goods")
public class GoodsController {

    @Resource
    private GoodsServiceImpl goodsService;

    // 分页查询商品
    @GetMapping("/page/{current}")
    public Vo<Goods> page(@PathVariable("current") Integer current) {
        Page<Goods> goodsPage = new Page<>(current >= 0 ? current : 1, 6);
        IPage<Goods> page = goodsService.page(goodsPage);
        if (page.getTotal() == 0) {
            // 表示没有数据直接返回
            return new Vo<>(false, "目前没有商品，请添加商品！");
        }
        // 数据
        List<Goods> goods = page.getRecords();
        // 当前页
        long curr = page.getCurrent();
        // 总记录数
        long total = page.getTotal();
        // 返回
        return new Vo<>(true, goods, curr, total);
    }

    // 新增
    @PostMapping("/insert")
    public Vo<Goods> insert(@RequestBody Goods goods) {
        boolean b = goodsService.save(goods);
        if (b) {
            // 新增成功，
            return new Vo<>(b, "新增成功！");
        } else {
            return new Vo<>(b,"新增失败，请稍后重试！");
        }
    }

    // 删除
    @GetMapping("/delete/{id}")
    public Vo<Goods> delete(@PathVariable("id") String id){
        boolean b = goodsService.removeById(id);
        if (b){
            return new Vo<>(b,"删除成功！");
        }else {
            return new Vo<>(b,"删除失败，请稍后重试！");
        }
    }
    // 按照商品名进行搜索
    @GetMapping("/search/{name}/{current}")
    public Vo<Goods> search(@PathVariable("name") String name,@PathVariable("current") Integer current){
        if (name.equals("")){
            return new Vo<>();
        }
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(Goods::getName,name);
        Page<Goods> goodsPage = new Page<>(current <= 0 ? 1 : current,6);
        IPage<Goods> page = goodsService.page(goodsPage, wrapper);
        if (page.getTotal() == 0){
            // 表示没有数据
            return new Vo<>(false,"目前没有该商品");
        }
        List<Goods> goods = page.getRecords();
        // 总条数
        long total = page.getTotal();
        // 当前页
        long pageCurrent = page.getCurrent();
        // 返回
        return new Vo<>(true,goods,pageCurrent,total);
    }
    // 修改
    @PostMapping("/updateGoods")
    public Vo<Goods> updateGoods(@RequestBody Goods goods){
        boolean b = goodsService.updateById(goods);
        if (b){
            return new Vo<>(b,"修改成功！");
        }else {
            return new Vo<>(b,"修改失败，请稍后重试");
        }
    }
    // 分类查询
    @GetMapping("/queryType/{goodstype}/{current}")
    public Vo<Goods> queryType(@PathVariable("goodstype") String type ,@PathVariable("current") Integer current){
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(StringUtils.isNotBlank(type),Goods::getGoodtype,type);
        Page<Goods> goodsPage = new Page<>(current <= 0 ? 1 : current,6);
        IPage<Goods> page = goodsService.page(goodsPage, wrapper);
        if (page.getTotal() == 0){
            return new Vo<>(false,"目前该分类没有商品！");
        }
        List<Goods> goods = page.getRecords();
        long current1 = page.getCurrent();
        long total = page.getTotal();
        return new Vo<>(true,goods,current1,total);
    }

    // 根据商品的id进行查询单个商品
    @GetMapping("/queryOne/{id}")
    public Vo<Goods> queryOne(@PathVariable("id")String id){
        LambdaQueryWrapper<Goods> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Goods::getId,id);
        Goods goods = goodsService.getOne(wrapper);
        return new Vo<>(true,goods);
    }
}
